package com.ruoyi.system.mc.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.mc.entity.BaseMsgEnum;
import com.ruoyi.system.mc.entity.McUserAddress;
import com.ruoyi.system.mc.entity.R;
import com.ruoyi.system.mc.mapper.McUserAddressMapper;
import com.ruoyi.system.mc.service.McUserAddressService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/*
 *  @author 雨安
 *  类名： McUserAddressServiceImpl
 *  创建时间：2024/5/27
 */
@Service
public class McUserAddressServiceImpl extends ServiceImpl<McUserAddressMapper,McUserAddress> implements McUserAddressService {

    @Resource
    private McUserAddressService mcUserAddressService;

    /**
     * 查询方法
     * @param mcUserAddress 查询条件对象
     * @return 返回查询结果集
     */
    @Override
    public R<McUserAddress> selectUserAddress(McUserAddress mcUserAddress) {
        //页码
        Integer current = 0;
        //行数
        Integer size = 0;
        /**
         * 分页插件暂时没研究明白配置弄在哪
         */
        if (mcUserAddress.getCurrentPage()!=0){
            //页码赋值
            current = (mcUserAddress.getCurrentPage()-1)*mcUserAddress.getPageSize();
            //总行数赋值
            size = mcUserAddress.getPageSize();
        }
        //查询数据
        List<McUserAddress> mcUserAddressList =
                mcUserAddressService.list(mcUserAddress.getLambdaQueryWrapper(mcUserAddress).last("limit " + current + "," + size));
        //查询总行数
        Long total = mcUserAddressService.count(mcUserAddress.getLambdaQueryWrapper(mcUserAddress));
        //返回数据集合
        return new R<>("查询成功",200,mcUserAddressList,total,current,size);
    }


    /**
     * 增加方法
     * @param mcUserAddress 添加数据对象
     * @return 返回增加结果
     */
    @Override
    public String insertUserAddress(McUserAddress mcUserAddress) {
        if (Objects.nonNull(mcUserAddress)){
            //创建日期格式转换对象
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            //设置默认值
            mcUserAddress.setUserId(SecurityUtils.getUserId());
            mcUserAddress.setCreateBy(SecurityUtils.getUsername());
            mcUserAddress.setCreateTime(java.sql.Date.valueOf(sdf.format(new Date())));
            mcUserAddress.setUpdateBy(SecurityUtils.getUsername());
            mcUserAddress.setUpdateTime(java.sql.Date.valueOf(sdf.format(new Date())));
            mcUserAddress.setDeleteFlag(Boolean.FALSE);
            //执行添加
            Boolean save = mcUserAddressService.save(mcUserAddress);
            //返回条件字符串
            return save == true ? BaseMsgEnum.SUCCESS.getGetMsg() : BaseMsgEnum.ERROR.getGetMsg();
        }else{
            //返回失败提示
            return BaseMsgEnum.ERROR.getGetMsg();
        }
    }

    /**
     * 修改方法
     * @param mcUserAddress 修改对象数据 通过ID修改
     * @return 返回修改结果集合
     */
    @Override
    public String updateUserAddressById(McUserAddress mcUserAddress) {
        //判断修改对象不为null
        if (Objects.nonNull(mcUserAddress)){
            //创建日期格式对象
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            //转换时间格式
            mcUserAddress.setUpdateTime(java.sql.Date.valueOf(sdf.format(new Date())));
            //设置用户ID
            mcUserAddress.setUserId(SecurityUtils.getUserId());
            //执行修改操作
            Boolean update = mcUserAddressService.updateById(mcUserAddress);
            //返回提示字符串
            return update == true ? BaseMsgEnum.SUCCESS.getGetMsg() : BaseMsgEnum.ERROR.getGetMsg();
        }else{
            //返回提示结果字符串
            return BaseMsgEnum.ERROR.getGetMsg();
        }
    }

    @Override
    public String deleteUserAddressById(Long addressId) {
        //判断是不是 null
        if (addressId != null && addressId != 0){
            //调用修改语句 进行逻辑删除
            Boolean update = mcUserAddressService.update(new UpdateWrapper<McUserAddress>()
                    //通过ID进行逻辑删除
                    .lambda().eq(McUserAddress::getAddressId, addressId)
                    //设置逻辑删除为 真
                    .set(McUserAddress::getDeleteFlag, Boolean.TRUE)
            );
            //判断是否修改成功
            return update == true ? BaseMsgEnum.SUCCESS.getGetMsg() : BaseMsgEnum.ERROR.getGetMsg();
        }else{
            //返回失败提示
            return BaseMsgEnum.ERROR.getGetMsg();
        }
    }


}
